#!/usr/bin/env python
#This is the main program. It should be *very* simple.
import subprocess, atexit
import commands, os

#the following lines establish a link with the camera via the USB port, these run 
#automatically when sx_main is excecuted
try: 
        if not os.path.exists('/tmp/sx_fifo'):
                dummy=subprocess.call('mkfifo /tmp/sx_fifo', shell=True)
        if not ('indiserver' and '/tmp/sx_fifo') in os.popen("ps aux").read():
                indiserver_process=subprocess.Popen(['indiserver','-f','/tmp/sx_fifo','-p','7777'],stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                #indiserver_process=subprocess.Popen('indiserver -f /tmp/sx_fifo -p 7777',shell=True)
                sxserver_process=subprocess.call('echo start indi_sx_ccd -n \"SX CCD\" > /tmp/sx_fifo', shell=True)
                #procs=[indiserver_process]
except Exception: print 'Unable to start indi server'


import sx_server
import sys
sys.path.append('../common')
import server_socket
import find_port
f = find_port.FindPort()
Port = f.findPort('sx')
sx=sx_server.SX()
ss=server_socket.ServerSocket(Port, 'sx', sx)	
ss.add_job(sx.checkIfFinished)
ss.run()
sx_server.indi.quit()
ss.close()
